#!/bin/bash

Upgrade_PHP()
{
    #Uninstall old_php
    apt-get -y remove php${Cur_PHP_Ver}-*
    apt-get -y autoremove
    PHP_prefix="php${PHP_Ver}"
    PHPFPMService="php${PHP_Ver}-fpm"
    PHPFPMProgram="php-fpm${PHP_Ver}"
PHPConfigFile="/etc/php/${PHP_Ver}/fpm/php.ini"
PHPCliConfigFile="/etc/php/${PHP_Ver}/cli/php.ini"
PHPFPMConfigFile="/etc/php/${PHP_Ver}/fpm/pool.d/www.conf"

    #Install
    apt-get -y install ${PHP_prefix}-fpm ${PHP_prefix}-mysql ${PHP_prefix}-gd ${PHP_prefix}-xsl ${PHP_prefix}-xmlrpc ${PHP_prefix}-curl ${PHP_prefix}-intl ${PHP_prefix}-tidy ${PHP_prefix}-mbstring ${PHP_prefix}-soap ${PHP_prefix}-zip ${PHP_prefix}-ldap
    echo -e " \e[0;32m[+] Modify PHP config ...\e[0m"
    sed -i 's/post_max_size = 8M/post_max_size = 2048M/g' ${PHPConfigFile}
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 2048M/g' ${PHPConfigFile}
    sed -i 's/;date.timezone =/date.timezone = PRC/g' ${PHPConfigFile}
    sed -i 's/short_open_tag = Off/short_open_tag = On/g' ${PHPConfigFile}
    sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' ${PHPConfigFile}
    sed -i 's/max_execution_time = 30/max_execution_time = 300/g' ${PHPConfigFile}
    sed -i 's/max_input_time = 60/max_input_time = 600/g' ${PHPConfigFile}
    sed -i 's/default_socket_timeout = 60/default_socket_timeout = 600/g' ${PHPConfigFile}
    sed -i 's/;max_input_vars = 1000/max_input_vars = 10000/g' ${PHPConfigFile}
    sed -i 's/memory_limit = 128M/memory_limit = 512M/g' ${PHPConfigFile}
    sed -i 's/;max_input_vars = 1000/max_input_vars = 10000/g' ${PHPCliConfigFile}
    #获取Nginx的用户名
    Nginx_User=$( ps axu | grep nginx | grep worker | sed -n '$p' | awk '{print $1}' )

    cat >${PHPFPMConfigFile}<<EOF
[www]
user = ${Nginx_User}
group = ${Nginx_User}
listen = /var/run/php-fpm.sock
listen.owner = ${Nginx_User}
listen.group = ${Nginx_User}
listen.mode = 0666
pm = dynamic
pm.max_children = ${PHPFPM_Max}
pm.start_servers = ${PHPFPM_Start}
pm.min_spare_servers = ${PHPFPM_Min}
pm.max_spare_servers = ${PHPFPM_Max}
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = /var/log/phpslow.log
EOF
    systemctl restart ${PHPFPMService}.service
    echo -e "\033[32m======== upgrade php completed ======\033[0m"
}

php_version=""
Cur_PHP_Version=$( /usr/bin/php -r 'echo PHP_VERSION;' )
Cur_PHP_major=`echo ${Cur_PHP_Version} | cut -d "." -f1`
Cur_PHP_minor=`echo ${Cur_PHP_Version} | cut -d "." -f2`
Cur_PHP_Ver=${Cur_PHP_major}.${Cur_PHP_minor}
echo "Current PHP Version:${Cur_PHP_Ver}"
echo "You can get version number from http://www.php.net/"
PHPSelect="1"
echo -e " \e[0;33mYou have many options for your PHP install.\e[0m"
echo -e "   \e[0;31m1\e[0m: Install PHP 5.6 supports Moodle 2.0-3.3"
echo -e "   \e[0;31m2\e[0m: Install PHP 7.0 supports Moodle 3.4-3.6"
echo -e "   \e[0;31m3\e[0m: Install PHP 7.1 supports Moodle 3.7-3.8"
echo -e "   \e[0;31m4\e[0m: Install PHP 7.2 supports Moodle 3.9-3.10"
echo -e "   \e[0;31m5\e[0m: Install PHP 7.3 supports Moodle 3.11-"
echo -e "   \e[0;31m6\e[0m: Install PHP 7.4 supports Moodle 3.11- \e[44;37m(Default)\e[0m"
echo -e "   \e[0;31m7\e[0m: Install PHP 8.0 supports Moodle 3.11- without filter_wiris"
read -p "Enter your choice (1, 2, 3, 4, 5, 6 or 7): " PHPSelect
if [ "${PHPSelect}" = "1" ]; then
    PHP_Ver="5.6"
elif [ "${PHPSelect}" = "2" ]; then
    PHP_Ver="7.0"
elif [ "${PHPSelect}" = "3" ]; then
    PHP_Ver="7.1"
elif [ "${PHPSelect}" = "4" ]; then
    PHP_Ver="7.2"
elif [ "${PHPSelect}" = "5" ]; then
    PHP_Ver="7.3"
elif [[ "${PHPSelect}" = "6" || "${PHPSelect}" = "" ]]; then
    PHP_Ver="7.4"
elif [ "${PHPSelect}" = "7" ]; then
    PHP_Ver="8.0"

fi
if [ "${PHP_Ver}" = "${Cur_PHP_Ver}" ]; then
    echo -e "\e[0;31mPHP \t\t\e[0;32m${PHP_Ver}\e[0m"
else
    echo -e "    \e[0;31mPHP\t\t\e[0mfrom \e[0;33m${Cur_PHP_Ver} \e[0;34m to \e[0;32m${PHP_Ver}\e[0m"
    Upgrade_PHP
fi